package ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import core.MenuItemType;

import ui.handlers.OrderHandler;

import java.awt.*;

import java.rmi.RemoteException;
import java.util.HashMap;

public class PlaceOrderScreen  extends BaseScreen {

	private JButton submit_btn;
	private JButton cancel_btn;
	private JFrame mainFrame;
	
	private HashMap<Integer, QuantityBox> quantityBoxes;
	
	private String customerUsername;
	private boolean isDeliveryOrder;
	private int orderId;
	
	/* Methods needed by handler */
	
	public JButton getSubmitButton()
	{ return submit_btn; }
	
	public JButton getCancelButton()
	{ return cancel_btn; }
	
	public HashMap<Integer, QuantityBox> getQuantityBoxes()
	{ return quantityBoxes; }
	
	public int getOrderId()
	{ return orderId; }
	
	public void setOrderId(int orderId)
	{ this.orderId = orderId; }
	
	public JFrame getMainFrame()
	{ return mainFrame; }
	
	
	/* END OF HANDLER METHODS */
	
	public PlaceOrderScreen(String customerUsername, boolean delivery) throws RemoteException
	{
		this.customerUsername = customerUsername;
		this.orderId = store.startNewOrder(customerUsername, delivery);
		
		setupQuantityBoxLists();
		
		mainFrame = new JFrame();
		mainFrame.setTitle("Place Order");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new BorderLayout());
		
		doTitle();
		doMain();
		doBottom();
		
		mainFrame.setVisible(true);
	}
	
	private void setupQuantityBoxLists() throws RemoteException
	{
		quantityBoxes = new HashMap<Integer, QuantityBox>();

		for(Integer itemId : store.getMenuItems()){
			quantityBoxes.put(itemId, new QuantityBox(itemId));
		}
	}
	
	private void doTitle()
	{
		String placeOrder = "<HTML><FONT SIZE = \"6\">Place Order</FONT></HTML>";
		JLabel placeOrder_lbl = new JLabel(placeOrder);
		JLabel quanity_lbl = new JLabel("Quanity");
		quanity_lbl.setBorder(new EmptyBorder(35, 0, 0, 135));
		
		JPanel title_panel = new JPanel();
		title_panel.setLayout(new BorderLayout());
		
		title_panel.add(placeOrder_lbl, BorderLayout.WEST);
		title_panel.add(quanity_lbl, BorderLayout.EAST);
		
		mainFrame.add(title_panel, BorderLayout.NORTH);
	}
	
	private void doMain() throws RemoteException
	{		
		JPanel mainContent_panel = new JPanel();
		int itemSlots = store.getNumberMenuItems() + store.getMenuItemTypeValues().length;
		mainContent_panel.setLayout(new GridLayout(itemSlots, 1));
		for(MenuItemType type : store.getMenuItemTypeValues()){
			JLabel typeLabel = new JLabel("<HTML><FONT SIZE = \"5\">" + type + "</FONT></HTML>");
			JPanel typePanel = new JPanel();
			typePanel.add(typeLabel);
			mainContent_panel.add(typePanel);
			
			for(int itemId : store.getMenuItemsByType(type)){
				JPanel temp_panel = new JPanel();
				temp_panel.setLayout(new GridLayout(1, 2));
				JPanel item_panel = new JPanel();
				item_panel.setLayout(new GridLayout(2, 1));
				item_panel.setBorder(new EmptyBorder(0, 0, 10, 0));
				
				JLabel name_lbl = new JLabel(store.getMenuItemName(itemId));
				JLabel desc_lbl = new JLabel(store.getMenuItemDescription(itemId));
				
				item_panel.add(name_lbl);
				item_panel.add(desc_lbl);
				
				JLabel number_lbl = new JLabel(store.getMenuItemPrice(itemId));
				JPanel quantity_panel = new JPanel();
				quantityBoxes.get(itemId).setText("0");
				quantity_panel.add(quantityBoxes.get(itemId));
				
				JPanel priceAndQuantity_panel = new JPanel();
				priceAndQuantity_panel.setLayout(new FlowLayout());
				priceAndQuantity_panel.add(number_lbl);
				priceAndQuantity_panel.add(quantity_panel);
				
				//add all components into one-line container
				temp_panel.add(item_panel);
				temp_panel.add(priceAndQuantity_panel);
				
				mainContent_panel.add(temp_panel);
			}
		}
	
		JScrollPane mainContent_scroll = new JScrollPane(mainContent_panel);
		mainFrame.add(mainContent_scroll, BorderLayout.CENTER);
	}
	
	private void doBottom()
	{
		submit_btn = new JButton("Submit");
		submit_btn.addActionListener(new OrderHandler(this));
		cancel_btn = new JButton("Cancel");
		cancel_btn.addActionListener(new OrderHandler(this));
		
		String delivery = "";
		if (isDeliveryOrder)
		{ delivery = "Delivery"; }
		
		JLabel customer_lbl = null;
		try {
			customer_lbl = new JLabel(store.getCustomerName(customerUsername), SwingConstants.LEFT);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		customer_lbl.setBorder(new EmptyBorder(0, 0, 0, 30));
		JPanel customer_panel = new JPanel();
		customer_panel.setLayout(new FlowLayout());
		customer_panel.add(customer_lbl);
		customer_panel.add(new JLabel(delivery));
		
		JPanel submit_panel = new JPanel();
		submit_panel.setBorder(new EmptyBorder(5, 20, 5, 75));
		submit_panel.add(submit_btn);
		
		JPanel cancel_panel = new JPanel();
		cancel_panel.setBorder(new EmptyBorder(5, 0, 5, 20));
		cancel_panel.add(cancel_btn);
		
		JPanel btn_panel = new JPanel();
		btn_panel.setLayout(new FlowLayout());
		btn_panel.add(cancel_panel);
		btn_panel.add(submit_panel);
		btn_panel.add(customer_panel);
		
		mainFrame.add(btn_panel, BorderLayout.SOUTH);
	}
}